package com.chanyachting.emall.action.yellow;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Businesscategory;
import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Companykeyword;
import com.chanyachting.emall.domain.Companyproductcategory;
import com.chanyachting.emall.domain.Productcategory;
import com.chanyachting.emall.form.YellowSearchForm;
import com.chanyachting.emall.service.BusinesscategoryService;
import com.chanyachting.emall.service.CompanyproductcategoryService;
import com.chanyachting.emall.service.ProductcategoryService;
import com.chanyachting.emall.serviceImpl.BusinesscategoryServiceImpl;
import com.chanyachting.emall.serviceImpl.CompanyServiceImpl;
import com.chanyachting.emall.serviceImpl.CompanyproductcategoryServiceImpl;
import com.chanyachting.emall.serviceImpl.ProductcategoryServiceImpl;
import com.chanyachting.emall.util.PageUtil;

public class QueryYellowPagesAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		int COMMON_PAGE_SIZE = 4;
		YellowSearchForm tForm = (YellowSearchForm) form;
		CompanyServiceImpl naservice = new CompanyServiceImpl();
		ProductcategoryService pcService = new ProductcategoryServiceImpl();
		CompanyproductcategoryService cpcService = new CompanyproductcategoryServiceImpl();
		BusinesscategoryService bcService = new BusinesscategoryServiceImpl();
		Businesscategory bc = null;

		List cpcList = null;
		List companyList = new ArrayList();
		List pcList = null;
		List ckwordList = null;
		int currentPage = tForm.getCurrentPage();
		int pageSize = COMMON_PAGE_SIZE;
		
		String keyword = tForm.getKeyword();
		System.out.println("keyword" + keyword);
		if(null != keyword && !"".equals(keyword)){
			keyword = keyword.replace(' ', '%');
			
			ckwordList = pcService.getCompanyByProductcategoryName(keyword);
			if(null != ckwordList && ckwordList.size()>0){
				for (int i = 0; i < ckwordList.size(); i++) {
					Company company = ((Companykeyword)ckwordList.get(i)).getCompany();
					// 判断公司是否已经加到公司列表中
					if (!hasCompany(company, companyList)) {
						if (null != company) {

							// 主营产品/服务set到company对象中
							company
									.setProductcategoryNames(getProductcategoryNames(cpcService
											.getProductcategoryNameByCompanyId(company
													.getId()
													+ "")));

							companyList.add(company);
						}
					}
				}
			}
			
		}
		
		String productcategoryId = request.getParameter("productcategoryId");
		String bcId = request.getParameter("bcId");
		
		if (null != bcId && !"".equals(bcId)) {
			bc = bcService.getBusinesscategoryById(bcId);
			pcList = bcService.getProductcategoryByBusinesscategory(bc);

			for (int i = 0; i < pcList.size(); i++) {
				Productcategory pc = null;
				pc = (Productcategory) pcList.get(i);

				cpcList = cpcService
						.getCompanyproductcategoryByProductcategoryId(pc
								.getId()
								+ "");
				for (int k = 0; k < cpcList.size(); k++) {
					Companyproductcategory cpc = (Companyproductcategory) cpcList
							.get(k);
					Company company = cpc.getCompany();
					// 判断公司是否已经加到公司列表中
					if (!hasCompany(company, companyList)) {
						
						System.out.println(companyList.size());
						if (null != company) {

							// 主营产品/服务set到company对象中
							company
									.setProductcategoryNames(getProductcategoryNames(cpcService
											.getProductcategoryNameByCompanyId(company
													.getId()
													+ "")));

							companyList.add(company);
						}
					}
				}

			}

		}

		if (null != productcategoryId && !"".equals(productcategoryId)) {
			cpcList = cpcService
					.getCompanyproductcategoryByProductcategoryId(productcategoryId);
			for (int i = 0; i < cpcList.size(); i++) {
				Companyproductcategory cpc = (Companyproductcategory) cpcList
						.get(i);
				Company company = cpc.getCompany();

				// 判断公司是否已经加到公司列表中
				if (!hasCompany(company, companyList)) {

					if (null != company) {
						// 主营产品/服务set到company对象中
						company
								.setProductcategoryNames(getProductcategoryNames(cpcService
										.getProductcategoryNameByCompanyId(company
												.getId()
												+ "")));
						companyList.add(company);
					}
				}
			}
		}
		int pageCount = 1;
		request.setAttribute("companyList", companyList);// 把公司列表set到request中
		String toPageAct = PageUtil.getGotoAction(request.getRequestURL().toString(), request.getParameterMap());
		String linkStr = PageUtil.getFy3(pageCount, currentPage, toPageAct);
		String selectionStr = PageUtil.getFy4(pageCount, currentPage, toPageAct);
		tForm.setLinkStr(linkStr);
		tForm.setSelectionStr(selectionStr);
		
		return mapping.findForward("yellowPage");
	}

	private String getProductcategoryNames(List list) {

		String names = "";
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size() - 1; i++) {
				names += ((Productcategory) list.get(i)).getName() + ",";
			}
			names += ((Productcategory) list.get(list.size() - 1)).getName();
		}

		return names;
	}

	// 判断是否重复添加公司到列表中
	private boolean hasCompany(Company company, List companyList) {
		if (null == company || null == companyList || companyList.size() == 0) {
			return false;
		}
		for (int i = 0; i < companyList.size(); i++) {
			
			if (company.getId().intValue() == ((Company) companyList.get(i)).getId().intValue()) {
				
				return true;
			}
		}
		return false;
	}
}
